﻿namespace Yipusoft.Account.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Yipusoft.Account.DBUtility;
    using Yipusoft.Account.Model;

    public class Role
    {
        public bool AddMenu(string RoleID, string MenuID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50), new SqlParameter("@MenuID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            commandParameters[1].Value = MenuID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_AddMenu", commandParameters) > 0);
            }
        }

        public bool AddPermission(string RoleID, string PermissionID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50), new SqlParameter("@PermissionID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            commandParameters[1].Value = PermissionID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_AddPermission", commandParameters) > 0);
            }
        }

        public bool Delete(string RoleID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_Delete", commandParameters) > 0);
            }
        }

        public bool Exsits(string RoleID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Roles_Exsits", commandParameters).Read();
            }
        }

        public List<RoleInfo> GetList(string StrWhere)
        {
            List<RoleInfo> list = new List<RoleInfo>();
            RoleInfo item = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@StrWhere", SqlDbType.NVarChar, 0xfa0) };
            commandParameters[0].Value = StrWhere;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Roles_GetList", commandParameters);
                while (reader.Read())
                {
                    item = new RoleInfo();
                    item.RoleID = reader.GetString(0);
                    item.RoleName = reader.GetString(1);
                    item.IsSystem = reader.GetBoolean(2);
                    item.Description = reader.GetString(3);
                    list.Add(item);
                }
                return list;
            }
        }

        public List<MenuInfo> GetMenuList(string RoleID)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            MenuInfo item = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Roles_GetMenuList", commandParameters);
                while (reader.Read())
                {
                    item = new MenuInfo();
                    item.MenuID = reader.GetString(0);
                    item.ParentID = reader.GetString(1);
                    item.MenuLevel = reader.GetInt32(2);
                    item.MenuName = reader.GetString(3);
                    item.MenuUrl = reader.GetString(4);
                    item.IsBlank = reader.GetBoolean(5);
                    item.MenuImage = reader.GetString(6);
                    item.SeqID = reader.GetInt32(7);
                    item.Description = reader.GetString(8);
                    list.Add(item);
                }
                return list;
            }
        }

        public List<PermissionInfo> GetPermissionList(string RoleID)
        {
            List<PermissionInfo> list = new List<PermissionInfo>();
            PermissionInfo item = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Roles_GetPermissionList", commandParameters);
                while (reader.Read())
                {
                    item = new PermissionInfo();
                    item.PermissionID = reader.GetString(0);
                    item.PermissionName = reader.GetString(1);
                    item.CategoryID = reader.GetString(2);
                    item.Description = reader.GetString(3);
                    list.Add(item);
                }
                return list;
            }
        }

        public RoleInfo GetRole(string RoleID)
        {
            RoleInfo info = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_Roles_GetDetail", commandParameters);
                if (reader.Read())
                {
                    info = new RoleInfo();
                    info.RoleID = reader.GetString(0);
                    info.RoleName = reader.GetString(1);
                    info.IsSystem = reader.GetBoolean(2);
                    info.Description = reader.GetString(3);
                }
                return info;
            }
        }

        public bool Insert(RoleInfo Role)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50), new SqlParameter("@RoleName", SqlDbType.NVarChar, 0xff), new SqlParameter("@IsSystem", SqlDbType.Bit), new SqlParameter("@Description", SqlDbType.NVarChar, 0xff) };
            commandParameters[0].Value = Role.RoleID;
            commandParameters[1].Value = Role.RoleName;
            commandParameters[2].Value = Role.IsSystem;
            commandParameters[3].Value = Role.Description;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_Insert", commandParameters) > 0);
            }
        }

        public bool RemoveMenu(string RoleID, string MenuID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50), new SqlParameter("@MenuID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            commandParameters[1].Value = MenuID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_RemoveMenu", commandParameters) > 0);
            }
        }

        public bool RemovePermission(string RoleID, string PermissionID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50), new SqlParameter("@PermissionID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = RoleID;
            commandParameters[1].Value = PermissionID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_RemovePermission", commandParameters) > 0);
            }
        }

        public bool Update(RoleInfo Role)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.NVarChar, 50), new SqlParameter("@RoleName", SqlDbType.NVarChar, 0xff), new SqlParameter("@IsSystem", SqlDbType.Bit), new SqlParameter("@Description", SqlDbType.NVarChar, 0xff) };
            commandParameters[0].Value = Role.RoleID;
            commandParameters[1].Value = Role.RoleName;
            commandParameters[2].Value = Role.IsSystem;
            commandParameters[3].Value = Role.Description;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_Roles_Update", commandParameters) > 0);
            }
        }
    }
}

